package com.work.keep.base;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.work.keep.activity.ActivityControl;


/**
 * Created by dllo on 16/1/7.
 */
public abstract class BaseActivity extends AppCompatActivity {

    protected static final String TAG = BaseActivity.class.getSimpleName();
    protected String tag = getClass().getSimpleName();
    //进度对话框
    private ProgressDialog dialog;


    //简化的findViewById()方法
    protected <T extends View> T findView(int resId) {
        T t = (T) findViewById(resId);
        return t;
    }

    //onCreate方法
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initPresenter();
        if (getLayout() != 0) {
            setContentView(getLayout());
        }else {
            setContentView(getLayoutView());
        }
        //调用ActivityControl的添加方法
        ActivityControl.addActivity(tag, this);
        dialog = new ProgressDialog(this);
        dialog.setTitle("请稍后");
        dialog.setMessage("数据加载中...");
        initView();
        initData();
    }

    protected abstract View getLayoutView();

    protected void initPresenter() {

    }

    //加载布局
    protected abstract int getLayout();

    //加载view
    protected abstract void initView();

    //加载数据
    protected abstract void initData();

    protected void showDialog() {
        dialog.show();
    }

    protected void dismissDialog() {
        dialog.dismiss();
    }

    protected void toastError() {
        Toast.makeText(this, "数据加载失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityControl.removeActivity(tag);
    }
}

